package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.system.domain.PAnswer;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.PAnswerVo;
import com.ruoyi.system.domain.bo.PAnswerBo;
import com.ruoyi.system.service.IPAnswerService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 问题答案Controller
 *
 * @author ruoyi
 * @date 2022-07-19
 */
@Validated
@Api(value = "问题答案控制器", tags = {"问题答案管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/answer")
public class PAnswerController extends BaseController {

    private final IPAnswerService iPAnswerService;

    /**
     * 查询问题答案列表
     */
    @ApiOperation("查询问题答案列表")
    @SaCheckPermission("system:answer:list")
    @GetMapping("/list")
    public TableDataInfo<PAnswer> list(PAnswer bo, PageQuery pageQuery) {
        return iPAnswerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出问题答案列表
     */
    @ApiOperation("导出问题答案列表")
    @SaCheckPermission("system:answer:export")
    @Log(title = "问题答案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PAnswerBo bo, HttpServletResponse response) {
        List<PAnswerVo> list = iPAnswerService.queryList(bo);
        ExcelUtil.exportExcel(list, "问题答案", PAnswerVo.class, response);
    }

    /**
     * 获取问题答案详细信息
     */
    @ApiOperation("获取问题答案详细信息")
    @SaCheckPermission("system:answer:query")
    @GetMapping("/{id}")
    public R<PAnswer> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iPAnswerService.queryById(id));
    }

    /**
     * 新增问题答案
     */
    @ApiOperation("新增问题答案")
    @SaCheckPermission("system:answer:add")
    @Log(title = "问题答案", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PAnswerBo bo) {
        return toAjax(iPAnswerService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改问题答案
     */
    @ApiOperation("修改问题答案")
    @SaCheckPermission("system:answer:edit")
    @Log(title = "问题答案", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PAnswerBo bo) {
        return toAjax(iPAnswerService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除问题答案
     */
    @ApiOperation("删除问题答案")
    @SaCheckPermission("system:answer:remove")
    @Log(title = "问题答案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iPAnswerService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
